package com.ruubypay.web.core.generator.plugins;

import java.util.ArrayList;
import java.util.List;

import org.mybatis.generator.api.GeneratedJavaFile;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
import org.mybatis.generator.api.dom.java.TopLevelClass;

/**
 * Desc : 自定义代码生产工具
 * @author sucyun 
 * Date :2018/3/17 下午3:57
 */
public class MybatisGeneratorPlugin extends PluginAdapter {
    private String servicePack;
    private String controllerPack;
    private String serviceImplPack;
    private FullyQualifiedJavaType controllerType;
    private FullyQualifiedJavaType serviceType;
    private String targetPath;

	@Override
	public boolean validate(List<String> arg0) {
		BizExtendPlugin.initialized(properties);

		controllerPack = properties.getProperty("controllerPackage");
		controllerType = new FullyQualifiedJavaType(controllerPack);
		servicePack = properties.getProperty("servicePackage");
		serviceType = new FullyQualifiedJavaType(servicePack);
		targetPath = properties.getProperty("targetPath");
		return true;
	}
	@Override
    public List<GeneratedJavaFile> contextGenerateAdditionalJavaFiles(IntrospectedTable introspectedTable) {
		List<GeneratedJavaFile> files = new ArrayList<>();
		String domainObjectName = introspectedTable.getTableConfiguration().getDomainObjectName();
		TopLevelClass controllerClass = new TopLevelClass(controllerType + "."+domainObjectName+ "Controller");
		TopLevelClass serviceClass = new TopLevelClass(serviceType + "."+domainObjectName+ "ServiceImpl");
		
		//创建 Controller
		BizExtendPlugin controllerPlugin = new MybatisControllerPlugin();
		controllerPlugin.createClassGenerated(controllerClass, introspectedTable, domainObjectName, files);
		//创建 Service
		BizExtendPlugin servicePlugin = new MybatisServicePlugin();
		servicePlugin.createClassGenerated(serviceClass, introspectedTable, domainObjectName, files);

		ViewGeneratorPlugin viewPlugin = new ViewGeneratorPlugin();
		viewPlugin.generator(domainObjectName, introspectedTable, targetPath);
		return files;
	}

}
